#include<bits/stdc++.h>
using namespace std;
inline long long read(){
    char ch=getchar();
    long long x=0,k=1;
    while(ch<'0'||ch>'9'){
        if(ch=='-') k=-1;
        ch=getchar();}
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*k;}
const int maxn=1000005;
long long a[maxn],l[maxn],r[maxn],tot[maxn];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    long long T;
    T=read();
    while(T--){
        long long sum=0,flag=0,t;
        memset(a,0,sizeof(a)),memset(l,0,sizeof(l)),memset(r,0,sizeof(r)),memset(tot,0,sizeof(tot));
        t=read();
        for(long long i=1;i<=t;i++) a[i]=read();
        for(long long i=1;i<=t;i++){
            for(long long j=i+1;j<=t;j++){
                if(a[i]==a[j]){
                    flag++,l[flag]=i,r[flag]=j;
                    break;}}}
        for(long long i=1;i<=flag;i++){
            for(long long j=1;j<=flag;j++){
                if(i==j) break;
                else{
                    if(r[j]-l[j]>1){
                        if(l[i]<l[j]<r[i]||l[j]<r[i]<r[j]) l[j]=0,r[j]=0;
                        if(l[i]>=l[j]&&r[i]<=r[j]) l[j]=0,r[j]=0;}}}//mistake
            for(long long i=1;i<=flag;i++) sum+=a[l[i]];
            tot[i]=sum,sum=0;}
        sort(tot+1,tot+flag+1),cout<<tot[flag]<<'\n';}
    return 0;}
//Sally
